import Vue from 'vue'
import Router from 'vue-router'
// import Login from "./components/Login.vue"
// import Home from "./components/Home.vue"
// import Welcome from "./components/Welcome.vue"
// import Users from "./components/user/Users.vue"
// import Rights from "./components/power/Rights.vue"
// import Roles from "./components/power/Roles.vue"
// import Params from "./components/goods/Params.vue"
// import Categories from "./components/goods/Categories.vue"
// import Goods from "./components/goods/Goods.vue"
// import AddGoods from "./components/goods/AddGoods.vue"
// import Orders from "./components/orders/Orders.vue"
// import Report from "./components/report/Report.vue"

const Login = () => import('./components/Login.vue')
const Home = () => import('./components/Home.vue')
const Welcome = () => import("./components/Welcome.vue")
const Users = () => import("./components/user/Users.vue")
const Rights = () => import("./components/power/Rights.vue")
const Roles = () => import("./components/power/Roles.vue")
const Params = () => import("./components/goods/Params.vue")
const Categories = () => import("./components/goods/Categories.vue")
const Goods = () => import("./components/goods/Goods.vue")
const AddGoods = () => import("./components/goods/AddGoods.vue")
const Orders = () => import("./components/orders/Orders.vue")
const Report = () => import("./components/report/Report.vue")



Vue.use(Router)

const  router = new Router({
    routes: [
        { path: "/", redirect: "/login" },//进行重定向到login页面
        { path: "/login", component: Login },
        { 
            path: "/home", 
            component: Home,
            redirect:'/Welcome',//重定向网页
            children:[//定义一个子路由
                { path: "/Welcome",component: Welcome },
                { path: "/users", component: Users },
                { path: "/rights", component: Rights},
                { path: "/roles", component: Roles},
                { path: "/params", component: Params},
                { path: "/categories", component: Categories},
                { path: "/goods", component: Goods},
                { path: "/goods/add", component: AddGoods },
                { path: "/orders", component: Orders},
                { path: "/reports", component: Report}



            ]
        },
        
    ]
});

// 为路由挂载路由导航守卫
router.beforeEach((to,from,next)=>{
    // to 表示即将访问的页面路径
    // from 表示从哪个页面跳转过来的
    // next 表示是一个函数，表示放行
    // next() 放行 next("/login") 强制跳转到login页面
    if(to.path === "/login"){
        return next();
    }
    // 获取token
    const tokenStr = window.sessionStorage.getItem("token");
    // 判断token是否存在
    if(!tokenStr){
        return next("/login");
    };
    next();
})

export default router